<template>
  <a-modal :title="title" :width="800" :visible="visible" :confirm-loading="confirmLoading" cancel-text="关闭" @ok="submit" @cancel="closeModal">
    <a-spin :spinning="confirmLoading">
      <a-form :form="formSpec">
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="规格值">
          <a-input :disabled="type == 2" v-decorator="['spec', validatorRules.val ]" placeholder="请输入规格值" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="价格">
          <a-input-number style="width: 200px" placeholder="单位（元），例：一分钱，输入：0.01" :formatter="(value) => `￥ ${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',')" :min="0" v-decorator="['price', validatorRules.val]" :precision="2" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="可用库存">
          <a-input-number style="width: 200px" :min="1" placeholder="最低为1" v-decorator="['quantity', validatorRules.val]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { Slime } from '@/mixins/slime';
import { evaFiltersMixin } from '@/mixins/evaMixin';
export default {
  name: 'goodsSpecAdd',
  mixins: [Slime, evaFiltersMixin],
  data() {
    return {
      title: '编辑',
      formSpec: this.$form.createForm(this),
      model: {},
      hospitalUl: [],
      validatorRules: {
        val: { rules: [{ required: true, message: '请输入内容!' }] },
      },
      type: 0, //1新增 2编辑
      id: null,
    };
  },
  created() {},
  methods: {
    // 增
    crudCreate() {
      this.type = 1
      this.visible = true
    },
    // 改
    crudUpdate(record) {
      let that = this
      this.formSpec.resetFields();
      this.type = 2
      this.visible = true
      setTimeout(()=>{
        that.formSpec.setFieldsValue({
          spec: record.spec,
          price: parseInt(record.price) / 100,
          quantity: record.quantity,
        });
        that.id = record.id
      })
      
    },
    // 提交
    submit() {
      const that = this;
      this.formSpec.validateFields((err, values) => {
        if (!err) {
          let param = {
            ...values,
            price: Math.round(values.price*100),
          };
          if (this.type == 1) {
            that.submitCreate(param);
          } else {
            param = {
              ...param,
              id: that.id,
            }
            that.submitUpdate(param);
          }
        }
      });
    },
    // 提交 增
    submitCreate(param) {
      this.$emit("createParam",param)
      this.$message.success('新增成功');
      this.closeModal();
    },
    // 提交 改
    submitUpdate(param) {
      this.$emit("updateParam",param)
      this.$message.success('编辑成功');
      this.closeModal();
    },
    // 弹窗-关闭
    closeModal() {
      this.confirmLoading = false;
      this.visible = false;
      this.id = null;
      this.formSpec.resetFields();
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>